import soya
import sys
import pickle
soya.path.append("./Soya")
soya.init("Raumschass Chess", sound = 0)

scene = soya.World()
sword_model = soya.Model.get("sword")
sword = soya.Body(scene, sword_model)
sword.x = 1.0
sword.rotate_y(90.0)
light = soya.Light(scene)
light.set_xyz(0.5, 0.0, 2.0)
camera = soya.Camera(scene)
camera.z = 2.0
soya.set_root_widget(camera)
soya.MainLoop(scene).main_loop()

class Settings():
    def __init__(self):
        SettingsFile = open("settings.ini","rw")
    
    def SetDefaults(self):
        defaults ={WhiteR:1,WhiteG:1,WhiteB:1,WhiteA:0.1,BlackR:0.1,BlackG:0.1,BlackB:0.1,BlackA:0.1}
        pickle.dump(defaults, SettingsFile)
    def getSettings(self):
        self.settings = pickle.load(SettingsFile)
        return self.settings
    
class ChessTile(TileNum):
    """a datatype for storing information about a chess tile in ram, this way a """
    
    """use pythagoran reduction to determine chess tile colour (123 = 1+2+3 = 6 and even)"""
    
    def __init__(self,TileNum):
        self.Piece = 0
        self.Colour = 0
        self.ColourInt = 0
        self.TileNumReduced = TileNum[1] + TileNum[2] + TileNum[3]
        if TileNumReduced%2==0:
            self.Colour = "White"
            self.ColourInt = 1
        else:
            self.Colour = "Black"
            self.ColourInt = 0
    def getColour():
        return self.Colour
    def getColourInt():
        return self.ColourInt
    def setPiece(Piece):
        self.Piece = Piece
        return 1
    def getPiece():
        return self.Piece

class RenderChessboard():
    def __init__(self):
        return 1
    def RenderTile():
        self.TileWorld = Soya.World(scene)
        soya.Face(TileWorld,[soya.Vertex(TileWorld,  0.5, -0.5, 0.1),
            soya.Vertex(TileWorld, -0.5, -0.5, 0.1),
            soya.Vertex(TileWorld, -0.5, -0.5, 0.1),
            soya.Vertex(TileWorld,  0.5, -0.5, 0.1),
            soya.Vertex(TileWorld,  0.5, -0.5, -0.1),
            soya.Vertex(TileWorld, -0.5, -0.5, -0.1),
            soya.Vertex(TileWorld, -0.5, -0.5, -0.1),
            soya.Vertex(TileWorld,  0.5, -0.5, -0.1),
            ])
        self.TileWorld.filename('tile.mdl')
        self.TileWorld.save()
    def RenderWhiteTile():
        self.settings = Settings().getSettings()
        self.red = settings['BlackR']
        self.green = settings['BlackG']
        self.blue = settings['BlackB']
        self.alpha = settings['BlackA']
        
        self.WhiteTileWorld = Soya.World(scene)
        
        RightBackTop = soya.Vertex(TileWorld, 0.5, -0.5, 0.1, diffuse = self.red,self.green,self.blue,self.alpha)
        LeftBackTop = soya.Vertex(self.WhiteTileWorld, -0.5, -0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftFrontTop = soya.Vertex(self.WhiteTileWorld, -0.5, 0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightFrontTop = soya.Vertex(self.WhiteTileWorld, -0.5, 0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightBackBottom = soya.Vertex(self.WhiteTileWorld,  0.5, -0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftBackBottom = soya.Vertex(self.WhiteTileWorld, -0.5, -0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftFrontBottom = soya.Vertex(self.WhiteTileWorld, -0.5, 0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightFrontBottom = soya.Vertex(self.WhiteTileWorld, -0.5, 0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        
        #front
        soya.Face(self.WhiteTileWorld, [RightFrontTop, RightFrontBottom, LeftFrontBottom, LeftFrontTop])
        #left
        soya.Face(self.WhiteTileWorld, [LeftBackBottom, LeftBackTop, LeftFrontBottom, LeftFrontTop])
        #top
        soya.Face(self.WhiteTileWorld, [LeftBackTop, RightBackTop, LeftFrontTop, RightFrontTop])
        #bottom
        soya.Face(self.WhiteTileWorld, [LeftBackBottom, RightBackBottom, LeftFrontBottom, RightFrontBottom])
        #right
        soya.Face(self.WhiteTileWorld, [RightBackBottom, RightBackTop, RightFrontBottom, RightFrontTop])
        #back
        self.Face(self.WhiteTileWorld, [RightBackTop, RightBackBottom, LeftBackBottom, LeftBackTop])
        
        self.WhiteTileWorld.filename('WhiteTile.mdl')
        self.WhiteTileWorld.save()
        return self.WhiteTileWorld
    def RenderBlackTile():
        self.settings = Settings().getSettings()
        self.red = settings['BlackR']
        self.green = settings['BlackG']
        self.blue = settings['BlackB']
        self.alpha = settings['BlackA']
        
        self.BlackTileWorld = Soya.World(scene)
        
        RightBackTop = soya.Vertex(TileWorld,  0.5, -0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftBackTop = soya.Vertex(self.BlackTileWorld, -0.5, -0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftFrontTop = soya.Vertex(self.BlackTileWorld, -0.5, 0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightFrontTop = soya.Vertex(self.BlackTileWorld, -0.5, 0.5, 0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightBackBottom = soya.Vertex(self.BlackTileWorld,  0.5, -0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftBackBottom = soya.Vertex(self.BlackTileWorld, -0.5, -0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        LeftFrontBottom = soya.Vertex(self.BlackTileWorld, -0.5, 0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        RightFrontBottom = soya.Vertex(self.BlackTileWorld, -0.5, 0.5, -0.1, diffuse =self.red,self.green,self.blue,self.alpha)
        
        #front
        soya.Face(self.BlackTileWorld, [RightFrontTop, RightFrontBottom, LeftFrontBottom, LeftFrontTop])
        #left
        soya.Face(self.BlackTileWorld, [LeftBackBottom, LeftBackTop, LeftFrontBottom, LeftFrontTop])
        #top
        soya.Face(self.BlackTileWorld, [LeftBackTop, RightBackTop, LeftFrontTop, RightFrontTop])
        #bottom
        soya.Face(self.BlackTileWorld, [LeftBackBottom, RightBackBottom, LeftFrontBottom, RightFrontBottom])
        #right
        soya.Face(self.BlackTileWorld, [RightBackBottom, RightBackTop, RightFrontBottom, RightFrontTop])
        #back
        self.Face(self.BlackTileWorld, [RightBackTop, RightBackBottom, LeftBackBottom, LeftBackTop])
        
        self.BlackTileWorld.filename('BlackTile.mdl')
        self.BlackTileWorld.save()
        return self.BlackTileWorld
            
        def renderChessboard(ChessBoardNumber):
            ChessBCoord = ChessBoardNumber * 100
        

class Cursor(soya.Body):
    def __init__(self, parent, model = None):
        soya.Body.__init__(self, parent, model)
    def begin_round(self):
        soya.Body.begin_round(self)
        for event in soya.process_event():
            if event[0] == soya.sdlconst.MOUSEMOTION:
                self.mouse_pos = camera.coord2d_to_3d(event[1], event[2], -15.0)
                self.move(self.mouse_pos)
